
package com.viweb.storemanager.temp;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.JButton;

public class ScaleRoundButton extends JButton {

    private BufferedImage image;

    /**
     * Create a round button
     *
     */
    public ScaleRoundButton(BufferedImage image) {
        super();
        this.setBorderPainted(false);
        this.setMargin(new Insets(0, 0, 0, 0));
        this.setContentAreaFilled(false);
        this.image = image;
    }

    @Override
    public void paintComponent(Graphics g) {
        Dimension d = getSize();
        g.drawImage(image, 0, 0, d.width, d.height, null);

    }
    /**
     *  Hit detection.
     *
     */
    Shape shape;

    @Override
    /**
     * This function is used to prevent the events when users click on button which
     * it is not area of the button's image
     */
    public boolean contains(int x, int y) {
        // If the button has changed size, make a new shape object.
        if (shape == null || !shape.getBounds().equals(getBounds())) {
            shape = new Ellipse2D.Float(0, 0, this.getWidth(), this.getHeight());
        }

        return shape.contains(x, y);
    }
}
